﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Team4Mock.Entity;
namespace Team4Mock.UserControl.Organisation
{
    public partial class CreateOrg : System.Web.UI.UserControl
    {
        OrganisationEntity oe=new OrganisationEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dlNational.DataSource = oe.GetCountry();
                dlNational.DataBind();
                
                GetCheckboxList(oe.GetOrgSpec(), clOrgSpe);
                GetCheckboxList(oe.GetAccred(), clAcc);
                GetCheckboxList(oe.GetSerBenifitCapa(), clSerBenCap);
                GetCheckboxList(oe.GetSerBarrieCapa(), clSerBarCap);
                GetCheckboxList(oe.GetSerDisaCapa(), clSerDisCap);
                GetCheckboxList(oe.GetEthicCapa(), clSerEthCap);
                GetCheckboxList(oe.GetSerPersoCapa(), clSerPerCirCap);
                
            }
        }
        void GetCheckboxList(DataTable dt1, CheckBoxList cl)
        {
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                cl.Items.Add(new ListItem(dt1.Rows[i][1].ToString(), dt1.Rows[i][0].ToString()));
            }
        }

        protected void btnBack1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Organisation.aspx");
        }
        string GetValueCheckBoxList(CheckBoxList cl)
        {
            string lb = string.Empty;
            for (int i = 0; i < cl.Items.Count; i++)
            {
                if (cl.Items[i].Selected == true)
                {
                    lb += cl.Items[i].Value + "=";
                }
            }
            return lb;
        }

        protected void btnCreate1_Click(object sender, EventArgs e)
        {
            if (txtOrgName.Text == "" || txtShortDes.Text == "" || txtAdd1.Text == "" || txtPhone.Text == "" ||
                    hdidBus.Value == "" || hdsic.Value == "" || PostCode.Value == "")
            {
                lb.Text = "Input Mandatory fields";
            }
            else
            {
                oe.OrganizationName = txtOrgName.Text;
                if (oe.CheckName(oe) == true)
                {
                    lb.Text = "Organization Name exists";
                }
                else
                {
                    if (InsertOrg() == true)
                    {
                        int id = oe.GetID();
                        
                        InsertAcc(id);
                        InsertBarrier(id);
                        InsertBenefit(id);
                        InsertDisa(id);
                        InsertEthnic(id);
                        InsertPeso(id);
                        InsertSpec(id);
                        if (ckExpress.Checked == true)
                        {
                            InsertOEOIPro(id);
                            InsertOEOISer(id);
                        }
                        lb.Text = "Insert Success";
                    }
                    else
                    {
                        lb.Text = "Error while Insert Organization";
                    }
                }
            }

        }
        //Insert Org
        bool InsertOrg()
        {
            try
            {
                
                int contactid = int.Parse(idContact.Value);
                string idbus = hdidBus.Value;
                string postcode = PostCode.Value;
                string sic = hdsic.Value;
                string town = Town.Value;
                string county = County.Value;
                string national = dlNational.SelectedValue;

                oe.OrganizationName = txtOrgName.Text.Trim();
                oe.OrganizationShortDescription = txtShortDes.Text;
                oe.LeadContact = contactid;
                if (ckPreferred.Checked == true)
                    oe.PreferredOrganization = 1;
                else
                    oe.PreferredOrganization = 0;
                if (ckExpress.Checked == true)
                    oe.ExpressionOfOrganization = 1;
                else
                    oe.ExpressionOfOrganization = 0;
                oe.TypeOfBusiness = idbus;
                oe.SICCode = sic;
                oe.OrFullDescription = txtFullDess.Text;
                oe.AddressLine1 = txtAdd1.Text;
                oe.AddressLine2 = txtAdd2.Text;
                oe.AddressLine3 = txtAdd3.Text;
                oe.PostCode = postcode;
                oe.Town = town;
                oe.County = county;
                oe.Country = national;
                oe.PhoneNumber = txtPhone.Text;
                oe.Fax = txtFax.Text;
                oe.WebAddress = txtWeb.Text;
                oe.ChartyNumber = txtCharity.Text;
                oe.CompanyNumber = txtCompany.Text;
                oe.IsActive = 1;
                oe.Insert(oe);
                return true;
            }
            catch
            {
                return false;
            }
        }
        void InsertAcc(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clAcc);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count()-1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertAcc(oe);
            }
        }
        
        void InsertBarrier(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clSerBarCap);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertBarrier(oe);
            }
        }

        void InsertBenefit(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clSerBenCap);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertBenefit(oe);
            }
        }
        //Disa
        void InsertDisa(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clSerDisCap);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertDisa(oe);
            }
        }
        //Ethnic
        void InsertEthnic(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clSerEthCap);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertEthnic(oe);
            }
        }
        //Perso
        void InsertPeso(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clSerPerCirCap);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertPerso(oe);
            }
        }
        //Spec
        void InsertSpec(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(clOrgSpe);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertSpec(oe);
            }
        }
        void InsertOEOIPro(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(cklEOIPro);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertOEOIPro(oe);
            }
        }
        void InsertOEOISer(int id)
        {
            oe.Organizationid = id;
            string a = GetValueCheckBoxList(cklEOISer);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                oe.Refid = int.Parse(tmp[i]);
                oe.InsertOEOISer(oe);
            }
        }

        protected void ckExpress_CheckedChanged(object sender, EventArgs e)
        {
            if (ckExpress.Checked==true)
            {
                lbdiv.Text="<li><a href=\"#Create3\" class=\"tab\">Details3</a></li>";
                GetCheckboxList(oe.GetOEOIPro(), cklEOIPro);
                GetCheckboxList(oe.GetOEOISer(), cklEOISer);
            }
            else
            {
                lbdiv.Text = "";
            }
        }
    }
}